// pages/myteam/people/teampeople.js
const app = getApp()
var CONFIG = require("../../../config/config.js");
var share = require("../../../utils/share.js");
Page({

    /**
     * 页面的初始数据
     */
    data: {
        status:'', //1:禁用 2:启用 5-离职
        pushteam:false,
        classarr: [],  //校区
        classarr_index: 0,  //校区下标
        classarr_id: '',  //校区id
        classpart: [],  //校区下的部门
        classpart_index: 0,  //校区下的部门小标
        classpart_id: '',  //校区下的部门id
        page:0,
        teamlist:[],
        keyword:'',
        is_team: 0,
        is_depart: 0,
        teamArray:[],
        teamregin: ['', ''],//课程
        teamIndex: [0, 0],//课程下标
        team_title:'全部部门',
    },

    zyzlclick(e){
        var that = this;
        var type = e.currentTarget.dataset.type;
        that.setData({
            status:type,
            page:0
        })
        that.team();
    },
    // 点击搜索
    seacrchclick(e){
        var that = this;
        var keywork = e.detail.value.phone;
        that.setData({
            keyword:keywork,
            page:0
        })
        that.team();
        // console.log(e)
    },

    // 选择
    classChange(e){
        var that = this;
        var index = e.detail.value;
        that.setData({
            classpart_index: index,
            classpart_id: that.data.classpart[index].id
        })
        that.setData({
            page: 0
        })
        that.team();
    },
    // 点击选择部门
    classclick(){
        // console.log(899)
        var that = this;
        that.setData({
            pushteam:true,
        })
    },
    // 点击部门
    itemclass(e){
        var that = this;
        var item = e.target.dataset.item;
        var index = e.target.dataset.index;
        that.setData({
            classactive:index,
            pushteam: false,
        })
    },

    // 点击查看客户列表
    courlistclick(e){
        var that = this;
        var item = e.target.dataset.item;
        wx.navigateTo({
            url: '/pages/staff/mycustom?teamtype=2&consultant_id=' + item.admin_id+'',
        })
    },
    yejiclick(e){
        var that = this;
        var item = e.target.dataset.item;
        wx.navigateTo({
            url: '/pages/staff/truemyyeji?teamtype=2&consultant_id=' + item.admin_id +'',
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        var role = app.globalData.role;
        // "is_team": 0,//是否是校区管理员 0:不是 1:是"is_depart": 0,//是否是部门管理员
        var is_team = role.is_team;
        var is_depart = role.is_depart;
        // var is_team = 0;
        // var is_depart = 1;
        that.setData({
            is_team: is_team,
            is_depart: is_depart
        })
        if (Number(is_team)==1){
            // 获取校区
            that.allteams();
        }else{
            that.depart(1);
        }
        that.team();
    },
    teamChange(e){
        var that = this;
        // console.log(899)
        var firstid = e.detail.value[0];
        var secid = e.detail.value[1];
        var firsttitle = this.data.teamArray[0][firstid];
        var sectitle = this.data.teamArray[1][secid];
        var first_id = this.data.classarr[firstid].id;
        var sec_id = this.data.classpart[secid].id;
        if (sectitle == '全部部门'){
            var team_title = firsttitle;
        }else{
            var team_title = firsttitle + '_' + sectitle;
        }
        that.setData({
            teamregin: [firsttitle, sectitle],
            "teamIndex[0]": e.detail.value[0],
            "teamIndex[1]": e.detail.value[1],
            team_title: team_title,
            classarr_id: first_id,
            classpart_id: sec_id,
        })
        that.setData({
            page: 0
        })
        that.team();

    },  
    // 选择课程
    teamPickerChange: function (e) {
        // vm = this;
        var that = this;
        // console.log(899)
        switch (e.detail.column) {
            case 0:
                var list = [];
                // console.log(e.detail.value)
                if (Number(e.detail.value) > 0) {
                    var objarr = that.data.classarr[e.detail.value];
                    var id = objarr.id;
                    that.setData({
                        classarr_id: id,
                        classarr_index: e.detail.value,
                    })
                    that.depart(2);
                    that.setData({
                        "teamIndex[0]": e.detail.value,
                    })
                } else {
                    list.push('全部部门')
                    that.setData({
                        "teamArray[1]": list,
                        "teamIndex[0]": e.detail.value,
                        "teamIndex[1]": 0,
                    })
                }

        }
    },
    // 获取校区
    allteams(){
        var that = this;
        var obj = {
            phone: wx.getStorageSync('tel')
        }
        share.wxRequest('crmapi/Teams/allteams', obj, function (res) {
            if(Number(res.data.errcode)==0){
                var data = res.data.data;
                if(Number(data.length)==1){
                    var data1 = data;
                    // console.log(data1);
                    that.setData({
                        classarr_id: data1[0].id
                    })
                    that.depart(1);
                }else{
                    var obj1 = [
                        {
                            "id": '',//校区ID
                            "type": 2,
                            "title": "全部校区"//校区名称
                        }
                    ]
                    var data1 = obj1.concat(data);
                    var firarr = [];
                    for (var i = 0; i < data1.length; i++) {
                        firarr.push(data1[i].title);
                    }
                    var firall = [firarr, ['全部部门']];
                    that.setData({
                        teamArray: firall,
                    })
                }
                that.setData({
                    classarr:data1,
                    classarr_id:data1[0].id
                })
            }
        })
    },
    // 校区下的部门
    depart(type){
        var that = this;
        var obj = {
            phone: wx.getStorageSync('tel'),
            teamId: that.data.classarr_id
        }
        share.wxRequest('crmapi/Teams/depart', obj, function (res) {
            if(Number(res.data.errcode)==0){
                var data = res.data.data;
                var obj1 = [
                    {
                        "id": '',//校区ID
                        "name": "全部部门"//校区名称
                    }
                ]
                var data1 = obj1.concat(data)
                that.setData({
                    classpart: data1,
                })
                if(Number(type)==2){
                    var list = [];
                    for (var i = 0; i < data1.length; i++) {
                        list.push(data1[i].name)
                    }
                    that.setData({
                        "teamArray[1]": list,
                        "teamIndex[1]": 0,
                    })
                }
            }
        })
    },
    onShow(){
        var that = this;
        if (wx.getStorageSync('prma')) {
            wx.removeStorageSync('prma');
            wx.removeStorageSync('parmdata');
        }
    },

    // 团队成员
    team(){
        var that = this;
        wx.showLoading({
            title: '加载中~',
        })
        var data = {
            phone:wx.getStorageSync('tel'),
            teamId: that.data.classarr_id,//	否	int	校区ID
            departId: that.data.classpart_id,//	否	int	部门ID
            keyword: that.data.keyword,
            status: that.data.status,
        }
        share.wxRequest('crmapi/Teams/teamsList', data, function (res) {
            if(Number(res.data.errcode)==0){
                var data = res.data.data;
                that.setData({
                    teamlist:data
                })
            }
            wx.hideLoading()
        })
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        var that = this;
        // that.setData({
        //     page:Number(that.data.page) + 1
        // })
        // that.team();
    },


    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        return {
            title: CONFIG.SHARETITLE,
            path: CONFIG.PATHLOGIN
        }
    }
})